package com.android.lovely.room.search

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.android.lovely.room.CardConverter
import com.kotlin.extensions.appContext
import com.kotlin.extensions.coroutine.applicationScope
import kotlinx.coroutines.launch

/**
 *
 * @author why
 * @date 2025/3/13 14:35
 */
@Database(entities = [SearchEntity::class], version = 1)
@TypeConverters(CardConverter::class)
abstract class SearchDatabase : RoomDatabase() {

    companion object {
        private val instance by lazy {
            Room.databaseBuilder(appContext, SearchDatabase::class.java, "lovely.search")
                .apply {
                    allowMainThreadQueries()
                    enableMultiInstanceInvalidation()
                    fallbackToDestructiveMigration()
                    setQueryExecutor { applicationScope.launch { it.run() } }
                }.build()
        }

        val dao get() = instance.dao()

    }

    abstract fun dao(): SearchDao
}